package org.bizming.web.action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.bizming.base.BaseErrCode;
import org.bizming.base.BaseException;
import org.bizming.base.RecordConstants;
import org.bizming.record.model.Record;
import org.bizming.record.model.RecordType;
import org.bizming.record.service.RecordService;
import org.bizming.record.service.RecordTypeService;
import org.bizming.util.DateUtil;
import org.bizming.web.model.ext.RecordExt;
import org.bizming.web.util.WebUtil;

public class RecordAction {

	private RecordService recordService;
	private RecordTypeService recordTypeService;

	private int page = 1;
	private int pages = RecordConstants.LIMIT;

	private Record record;

	/**
	 * 添加记录
	 * 
	 * @return
	 */
	public String viewRecord() {
		List<RecordType> typelist = recordTypeService.selectList(1, 1000);
		WebUtil.getRequest().setAttribute("typelist", typelist);

		Record recordView = new Record();
		try {
			if (record != null && record.getId() != null) {
				recordView = recordService.get(record.getId());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		WebUtil.getRequest().setAttribute("record", recordView);
		return "view";
	}

	/**
	 * 修改记录
	 * 
	 * @return
	 */
	public String updateOrAddRecord() {
		int errCode = BaseErrCode.SUCCESS;
		try {
			if (record.getId() != null) {
				recordService.update(record);
			} else {
				recordService.add(record);
			}
			return selectList();
		} catch (BaseException e) {
			errCode = e.getErrCode();
		} catch (Exception e) {
			e.printStackTrace();
			errCode = BaseErrCode.UNKNOWN;
		}
		WebUtil.getRequest().setAttribute("record", record);
		WebUtil.getRequest().setAttribute("errCode", errCode);
		return "view";
	}

	/**
	 * 查看记录
	 * 
	 * @return
	 */
	public String selectList() {
		List<Record> list = recordService.selectList(page, pages);
		List<RecordExt> exts = new ArrayList<RecordExt>();
		if (list != null) {
			Calendar c = Calendar.getInstance();
			for (Record record : list) {
				RecordExt ext = new RecordExt();
				ext.setCreateTime(DateUtil.convert(record.getCreateTime(), c));

				ext.setRecord(record);
				ext.setType(recordTypeService.get(record.getTypeId()) == null ? ""
						: recordTypeService.get(record.getTypeId()).getType());
				exts.add(ext);
			}
		}

		int count = recordTypeService.count();

		WebUtil.getRequest().setAttribute("list", exts);
		WebUtil.getRequest().setAttribute("count", count);

		return "list";
	}

	// ===================================
	public RecordService getRecordService() {
		return recordService;
	}

	public void setRecordService(RecordService recordService) {
		this.recordService = recordService;
	}

	public RecordTypeService getRecordTypeService() {
		return recordTypeService;
	}

	public void setRecordTypeService(RecordTypeService recordTypeService) {
		this.recordTypeService = recordTypeService;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	public Record getRecord() {
		return record;
	}

	public void setRecord(Record record) {
		this.record = record;
	}

}
